﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace MvePublicService.ClipOperations
{
	/*
	 * 日本SGI株式会社
	 * ヨハンソン・ヘンリク
	 * johansson@sgi.co.jp
	 * Project: MVEPublicService
	 * 
	 * Comments:
	 *      Could have lived in the Keyword Namespace?
	 * 
	 * Change Log:
	 * ---------------------------------------------------------------------------
	 * 1/8/2009 2:12:50 PM		Johansson Henrik		Class Created.
	 * 1/13/2009 17:44:00 PM							Changed the SearchSegmentIdsByKeyword to take and pass on the
	 *													users Db Id to the database. This is to enable logging of
	 *													searches in the Db.
	 */

	/// <summary>
	/// Search for Clips and Segments in the Database using the methods
	/// available in this static class.
	/// </summary>
	/// <remarks>INSERT:Creds</remarks>
	internal static class SearchOperations
	{
		/// <summary>
		/// Finds the Ids of all segments that either contain the supplied keyword
		/// in its Title or Description, OR is linked to a Keyword that contains the 
		/// string supplied to the method.
		/// Requires that no other commands are open on the connection used to fetch the Ids.
		/// </summary>
		/// <param name="userDbId">Users DB id. Used for operation Logging.</param>
		/// <param name="keyword">The string to search for.</param>
		/// <param name="connection">Sql Connection</param>
		/// <returns>List Object Representation of the discovered Segment Ids.</returns>
		internal static List<int> SearchSegmentIdsByKeyword(int userDbId, string keyword, SqlConnection connection)
		{
			SqlCommand command = new SqlCommand();
			SqlDataReader reader = null;
			command.CommandTimeout = 60;

			List<int> result = new List<int>();

			try
			{
				command.Connection = connection;
				command.CommandType = System.Data.CommandType.StoredProcedure;
				command.CommandText = "GetSegmentIdsByKeyword";

				command.Parameters.Add("keyword", SqlDbType.VarChar, 64).Value = keyword;
				command.Parameters.Add("userId", SqlDbType.Int).Value = userDbId;
				reader = command.ExecuteReader();

				if (reader.HasRows)
				{
					while (reader.Read())
					{
						result.Add(reader.GetInt32(0));
					}
				}
			}
			catch (NullReferenceException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (SqlException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (InvalidOperationException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (IndexOutOfRangeException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			finally
			{
				command.Dispose();
				if (reader != null)
				{
					reader.Close();
					reader.Dispose();
				}
			}

			return result;
		}//end:SearchSegmentIdsByKeyword
	}
}
